home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / programming / other / jikes / src / system.cpp < prev    next >
C/C++ Source or Header  |  1999-05-14  |  22KB  |  573 lines

  1. // $Id: system.cpp,v 1.6 1999/02/12 14:39:13 shields Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10. #include "config.h"
  11. #include <sys/stat.h>
  12. #include "control.h"
  13. #include "semantic.h"
  14. #include "zip.h"
  15.  
  16. int Control::ConvertUnicodeToUtf8(wchar_t *source, char *target)
  17. {
  18.     int length = 0;
  19.  
  20.     for (; *source; source++)
  21.     {
  22.         int ch = *source;
  23.  
  24.         if (ch == 0)
  25.         {
  26.              target[length++] = (char) 0xC0;
  27.              target[length++] = (char) 0x80;
  28.         }
  29.         else if (ch <= 0x007F)
  30.              target[length++] = (char) ch;
  31.         else if (ch <= 0x07FF)
  32.         {
  33.              target[length++] = (char) ((char) 0xC0 | (char) ((ch >> 6) & 0x001F)); // bits 6-10
  34.              target[length++] = (char) ((char) 0x80 | (char) (ch & 0x003F));        // bits 0-5
  35.         }
  36.         else
  37.         {
  38.              target[length++] = (char) ((char) 0xE0 | (char) ((ch >> 12) & 0x000F));
  39.              target[length++] = (char) ((char) 0x80 | (char) ((ch >> 6) & 0x003F));
  40.              target[length++] = (char) ((char) 0x80 | (char) (ch & 0x3F));
  41.         }
  42.     }
  43.     target[length] = U_NULL;
  44.  
  45.     return length;
  46. }
  47.  
  48.  
  49. void Control::FindPathsToDirectory(PackageSymbol *package)
  50. {
  51.     if (package -> directory.Length() == 0)
  52.     {
  53.         PackageSymbol *owner_package = package -> owner;
  54.         if (owner_package) // package is a subpackage?
  55.         {
  56.             for (int i = 0; i < owner_package -> directory.Length(); i++)
  57.             {
  58.                 DirectorySymbol *owner_directory_symbol = owner_package -> directory[i],
  59.                                 *subdirectory_symbol = owner_directory_symbol -> FindDirectorySymbol(package -> Identity());
  60.                 if ((! subdirectory_symbol) && (! owner_directory_symbol -> IsZip()))
  61.                 {
  62.                     int length = owner_directory_symbol -> DirectoryNameLength() + package -> Utf8NameLength() + 1; // +1 for '/'
  63.                     char *directory_name = new char[length + 1]; // +1 for '\0';
  64.  
  65.                     strcpy(directory_name, owner_directory_symbol -> DirectoryName());
  66.                     if (owner_directory_symbol -> DirectoryName()[owner_directory_symbol -> DirectoryNameLength() - 1] != U_SLASH)
  67.                         strcat(directory_name, StringConstant::U8S__SL_);
  68.                     strcat(directory_name, package -> Utf8Name());
  69.  
  70.                     if (::SystemIsDirectory(directory_name))
  71.                         subdirectory_symbol = owner_directory_symbol -> InsertAndReadDirectorySymbol(package -> Identity());
  72.  
  73.                     delete [] directory_name;
  74.                 }
  75.  
  76.                 if (subdirectory_symbol)
  77.                     package -> directory.Next() = subdirectory_symbol;
  78.             }
  79.         }
  80.         else
  81.         {
  82.             //
  83.             // Recall that since classpath[0] contains the default directory, we always
  84.             // start searching at location 1.
  85.             //
  86.             for (int k = 1; k < classpath.Length(); k++)
  87.             {
  88.                 PathSymbol *path_symbol = classpath[k];
  89.                 DirectorySymbol *directory_symbol = path_symbol -> RootDirectory() -> FindDirectorySymbol(package -> Identity());
  90.                 if ((! directory_symbol) && (! path_symbol -> IsZip()))
  91.                 {
  92.                     int length = path_symbol -> Utf8NameLength() + package -> Utf8NameLength() + 1; // +1 for '/'
  93.                     char *directory_name = new char[length + 1]; // +1 for '/' +1 for '\0'
  94.                     strcpy(directory_name, path_symbol -> Utf8Name());
  95.                     strcat(directory_name, StringConstant::U8S__SL_);
  96.                     strcat(directory_name, package -> Utf8Name());
  97.  
  98.                     if (::SystemIsDirectory(directory_name))
  99.                         directory_symbol = path_symbol -> RootDirectory() -> InsertAndReadDirectorySymbol(package -> Identity());
  100.                     delete [] directory_name;
  101.                 }
  102.  
  103.                 if (directory_symbol)
  104.                     package -> directory.Next() = directory_symbol;
  105.             }
  106.         }
  107.     }
  108.  
  109.     return;
  110. }
  111.  
  112.  
  113. void Control::ProcessGlobalNameSymbols()
  114. {
  115.     this -> dot_name_symbol        = FindOrInsertName(US__DO_, wcslen(US__DO_));
  116.     this -> dot_dot_name_symbol    = FindOrInsertName(US__DO__DO_, wcslen(US__DO__DO_));
  117.     this -> length_name_symbol     = FindOrInsertName(US_length, wcslen(US_length));
  118.     this -> init_name_symbol       = FindOrInsertName(US__LT_init_GT_, wcslen(US__LT_init_GT_));
  119.     this -> clinit_name_symbol     = FindOrInsertName(US__LT_clinit_GT_, wcslen(US__LT_clinit_GT_));
  120.     this -> block_init_name_symbol = FindOrInsertName(US_block_DOLLAR, wcslen(US_block_DOLLAR));
  121.     this -> this0_name_symbol      = FindOrInsertName(US_this0, wcslen(US_this0));
  122.     this -> clone_name_symbol      = FindOrInsertName(US_clone, wcslen(US_clone));
  123.     this -> object_name_symbol     = FindOrInsertName(US_Object, wcslen(US_Object));
  124.     this -> type_name_symbol       = FindOrInsertName(US_TYPE, wcslen(US_TYPE));
  125.     this -> class_name_symbol      = FindOrInsertName(US__class_DOLLAR, wcslen(US__class_DOLLAR));
  126.  
  127.     return;
  128. }
  129.  
  130.  
  131. //
  132. // Create the unnamed package and set up global names.
  133. //
  134. void Control::ProcessUnnamedPackage()
  135. {
  136.     unnamed_package = external_table.InsertPackageSymbol(FindOrInsertName(US_EMPTY, wcslen(US_EMPTY)), NULL);
  137.  
  138.     //
  139.     // Create an entry for no_type. no_type is used primarily to signal an error
  140.     //
  141.     no_type = unnamed_package -> InsertSystemTypeSymbol(FindOrInsertName(US__QU__QU_, wcslen(US__QU__QU_)));
  142.     no_type -> SetSignature(Utf8_pool.FindOrInsert(U8S__DO_, strlen(U8S__DO_))); // give it some signature...
  143.     no_type -> outermost_type = no_type;
  144.     no_type -> SetOwner(unnamed_package);
  145.     no_type -> MarkBad();
  146.  
  147.     //
  148.     // Create an entry for the null type.
  149.     //
  150.     null_type = unnamed_package -> InsertSystemTypeSymbol(FindOrInsertName(US_null, wcslen(US_null)));
  151.     null_type -> outermost_type = null_type;
  152.     null_type -> SetOwner(unnamed_package);
  153.     null_type -> SetACC_PUBLIC();
  154.  
  155.     return;
  156. }
  157.  
  158.  
  159. void Control::ProcessPath()
  160. {
  161.     NameSymbol *dot_path_name_symbol;
  162.  
  163. #if defined(UNIX_FILE_SYSTEM) || defined(AMIGAOS_FILE_SYSTEM)
  164.     dot_path_name_symbol = dot_name_symbol;
  165. #elif defined(WIN32_FILE_SYSTEM) /*|| defined(AMIGAOS_FILE_SYSTEM)*/
  166.     char *main_current_directory = option.GetMainCurrentDirectory();
  167.     int dot_path_name_length = strlen(main_current_directory);
  168.     wchar_t *dot_path_name = new wchar_t[dot_path_name_length + 1];
  169.     for (int i = 0; i < dot_path_name_length; i++)
  170.         dot_path_name[i] = main_current_directory[i];
  171.     dot_path_name[dot_path_name_length] = U_NULL;
  172.     dot_path_name_symbol = FindOrInsertName(dot_path_name, dot_path_name_length);
  173.     delete [] dot_path_name;
  174. #endif
  175.  
  176.     //
  177.     // We need a place to start. Allocate a "." directory with no owner initially. (Hence, the null argument.)
  178.     // Allocate a "." path whose associated directory is the "." directory.
  179.     // Identify the "." path as the owner of the "." directory.
  180.     //
  181.     DirectorySymbol *dot_directory = new DirectorySymbol(dot_name_symbol, NULL);
  182.     classpath.Next() = classpath_table.InsertPathSymbol(dot_path_name_symbol, dot_directory);
  183.     dot_directory -> ResetDirectory(); // Note that main_root_directory is reset after it has been assigned an owner above
  184.     root_directories.Next() = dot_directory;
  185.  
  186.     //
  187.     //
  188.     //
  189.     if (option.classpath)
  190.     {
  191.         int max_path_name_length = strlen(option.classpath) + 1; // The longest possible path name we can encounter
  192.         wchar_t *path_name = new wchar_t[max_path_name_length + 1]; // +1 for '\0'
  193.  
  194.         wchar_t *input_name = NULL;
  195.         char *full_directory_name = NULL;
  196.  
  197.         for (char *path = option.classpath, *path_tail = &path[strlen(path)]; path < path_tail; path++)
  198.         {
  199. #ifdef WIN32_FILE_SYSTEM
  200.             delete []